package com.hjh.java.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test2 {

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Test2().testMethod();

        }
    }


    ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 10, 500, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    Integer aValue = new Integer(10000);
    String p = "1";


    public void testMethod() {
        int size = 1000;
        final CountDownLatch countDownLatch = new CountDownLatch(size);

        for (int i = 0; i < size; i++) {
            new Thread(new Runnable() {
                public void run() {
                    add(countDownLatch);
                }
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(aValue);
    }

    public void add(CountDownLatch cd) {
        synchronized (aValue) {
            aValue++;
            cd.countDown();
        }
    }
}
